package ipsite.server;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import ipsite.data.Data;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;


public class AgeDomain  extends Thread{
	DBObject domain;
	public AgeDomain(DBObject domain)
	{
		this.domain=domain;
		this.start();
	}
	public void run ()
	{
		
		String message = null;
		try {
			
			message = URLEncoder.encode(domain.get("name").toString(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

        try {
            URL url = new URL("http://www.seologs.com/dns/domain-check.html");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write("query=" + message);
            writer.write("&output=cdt");
            writer.close();
    
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            	 String content = "";
            	 char[] buf = new char[102102];
     			int numRead=0;
     	        while((numRead=reader.read(buf)) != -1){
     	            String readData = String.valueOf(buf, 0, numRead);
     	            content += readData;
     	            buf = new char[102102];
     	        }
     	        reader.close();
            	 Document doc = Jsoup.parse(content);
            	 String age = doc.select("div[class=KonaBody]").select("p").text();
            	 String[] ageSplit = age.split("(approximately)");
            	 if(ageSplit.length ==2)
            	 {
//            		 BasicDBObject info = new BasicDBObject();
//          			
//         			 DBCollection coll = Data.data("domain");
         			 domain.put("age", ageSplit[1].replaceAll("[):]+", ""));
//         			 domain.put("test", "test");
//        			 BasicDBObject set = new BasicDBObject("$set", info);
//        			 coll.update(domain, set);
            	 }
            } else {
                // Server returned HTTP error code.
            }
        } catch (MalformedURLException e) {
            // ...
        } catch (IOException e) {
            // ...
        }
	}
//	public static void main( String args[])
//	{
//		new AgeDomain("google.com");
//	}
}
